<?php

class Default_Model_SeatMapMapper {
	
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if(is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
	}
	
	public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_SeatMap');
        }
        return $this->_dbTable;
    }
    
	public function save(Default_Model_SeatMap $seatmap)
    {
        $data = array(
        	'bitmapUrl'	  => $seatmap->getBitmapUrl(),
        	'event'  => $seatmap->getEvent(),
        	'reservationDuration'  => $seatmap->getReservationDuration(),
        );

        if (null === ($id = $seatmap->getSeatmapId())) {
            unset($data['seatmapId']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('seatmapId = ?' => $id));
        }
    }
    
	public function find($id, Default_Model_SeatMap $seatmap)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $seatmap->setSeatmapId($row->seatmapId)
                  ->setBitmapUrl($row->BitmapUrl)
                  ->setEvent($row->event)
                  ->setReservationDuration($row->ReservationDuration);
    }
	
	public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_SeatMap();
            $entry->setSeatmapId($row->seatmapId)
                  ->setBitmapUrl($row->BitmapUrl)
                  ->setEvent($row->event)
                  ->setReservationDuration($row->ReservationDuration)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }

}

?>